<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClickSingle">{{$t('添加单出口策略')}}</el-button>
      <el-button type="success" size='small' @click="dialogMultiVisible()" class="addClickMore">{{$t('添加多出口策略')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="policyList" tooltip-effect="dark" style="width: 100%">
      <el-table-column prop="name" :label="$t('名称')" align='center'>
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.name" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="type" align='center' width="80" :label="$t('类型')">
        <template slot-scope="scope">
          {{ typeList[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column prop="ip" align='center' label="IPv4/IPv6" width="100"></el-table-column>
      <el-table-column prop="src" align='center' :label="$t('源IP')">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.src" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="dst" align='center' :label="$t('目的IP')">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.dst" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="ser" align='center' :label="$t('服务')">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.ser" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="app" align='center' :label="$t('应用')">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.app" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="devs" align='center' :label="$t('出接口')" :formatter="mutilDevFormatter"></el-table-column>
      <el-table-column prop="gw" align='center' :label="$t('下一跳')">
        <template slot-scope="scope">
          {{ scope.row['mutil-dev'][0].gw }}
        </template>
      </el-table-column>
      <el-table-column prop="balance" align='center' width="80" :label="$t('负载类型')">
        <template slot-scope="scope">
          {{ balanceFil(scope.row.balance) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" align='center' width="80" :label="$t('状态')">
        <template slot-scope="scope">
          {{ stateList[scope.row.state] }}
        </template>
      </el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="300">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="scope.row.type === 'multi' ? dialogMultiVisible(scope.row) : dialogVisible(scope.row)" :class="'edit' + (scope.$index + 1)">{{$t('编辑')}}</el-button>
          <el-button type="danger" plain size="mini" @click="deleteData(scope.row)" :class="'del' + (scope.$index + 1)">{{$t('删除')}}</el-button>
          <el-button type="info" plain size="mini" @click="viewData(scope.row)" :class="'finds' + (scope.$index + 1)">{{$t('查看')}}</el-button>
          <el-button type="primary" plain size="mini" @click="moveChangeDialogVisible(scope.row)" :class="'move' + (scope.$index + 1)">{{$t('移动')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <strategy-single-dialog v-if="strategySingleDialogVisible" ref="refStrategySingleDialogVisible"></strategy-single-dialog>
    <strategy-check-dialog v-if="strategyCheckDialogVisible" ref="refStrategyCheckDialogVisible"></strategy-check-dialog>
    <strategy-multi-dialog v-if="strategyMultiDialogVisible" ref="refStrategyMultiDialogVisible"></strategy-multi-dialog>
    <move-dialog v-if="moveDialogVisible" ref="refMoveDialogVisible"></move-dialog>
  </div>
</template>
<script>
import { getArray, delInfo } from '@/api/network/route/policyRoute'
import StrategySingleDialog from './components/StrategySingleDialog'
import moveDialog from './components/moveDialog'
import StrategyCheckDialog from './components/StrategyCheckDialog'
import StrategyMultiDialog from './components/StrategyMultiDialog'
// import 'view-design/dist/styles/iview.css'
export default {
  name: 'Strategy',
  components: {
    StrategySingleDialog,
    moveDialog,
    StrategyCheckDialog,
    StrategyMultiDialog
  },
  data () {
    return {
      strategySingleDialogVisible: false,
      strategyMultiDialogVisible: false,
      strategyCheckDialogVisible: false,
      moveDialogVisible: false,
      policyList: [],
      typeList: {
        'multi': this.$t('多出口'),
        'single': this.$t('单出口')
      },
      stateList: {
        'able': this.$t('开启'),
        'disable': this.$t('关闭')
      },
      balanceList: {
        'round': this.$t('轮询负载'),
        'width': this.$t('带宽负载'),
        'super': this.$t('最优路径')
      }
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // 列表
    listData () {
      getArray().then(res => {
        if (res.status === 0) {
          this.policyList = res.info
        }
      })
    },
    // 添加单出口策略
    dialogVisible (data) {
      this.strategySingleDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refStrategySingleDialogVisible.init(data)
      })
    },
    // 添加多出口策略
    dialogMultiVisible (data) {
      this.strategyMultiDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refStrategyMultiDialogVisible.init(data)
      })
    },
    balanceFil (data) {
      return data ? this.balanceList[data] : ''
    },
    viewData (data) {
      this.strategyCheckDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refStrategyCheckDialogVisible.init(data)
      })
    },
    // 移动按钮表单
    moveChangeDialogVisible (data) {
      this.moveDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refMoveDialogVisible.init(data)
      })
    },
    deleteData (row) {
      this.$confirm(this.$t('您是否要删除选中的数据？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        delInfo({ name: row.name }).then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('删除成功'),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    },
    mutilDevFormatter (row) {
      if (row.type === 'multi') {
        let test = ''
        row['mutil-dev'].forEach((item, index, array) => {
          if (index === 0) {
            test += item.dev
          } else {
            test += ',' + item.dev
          }
        })
        return test
      } else {
        return row['mutil-dev'][0].dev
      }
    }
  }
}
</script>
